package com.wao.cms.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.wao.cms.service.TemplatePropertyService;
import com.wao.cms.web.rest.util.HeaderUtil;
import com.wao.cms.web.rest.util.PaginationUtil;
import com.wao.cms.service.dto.TemplatePropertyDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing TemplateProperty.
 */
@RestController
@RequestMapping("/api")
public class TemplatePropertyResource {

    private final Logger log = LoggerFactory.getLogger(TemplatePropertyResource.class);

    private static final String ENTITY_NAME = "templateProperty";

    private final TemplatePropertyService templatePropertyService;

    public TemplatePropertyResource(TemplatePropertyService templatePropertyService) {
        this.templatePropertyService = templatePropertyService;
    }

    /**
     * POST  /template-properties : Create a new templateProperty.
     *
     * @param templatePropertyDTO the templatePropertyDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new templatePropertyDTO, or with status 400 (Bad Request) if the templateProperty has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/template-properties")
    @Timed
    public ResponseEntity<TemplatePropertyDTO> createTemplateProperty(@Valid @RequestBody TemplatePropertyDTO templatePropertyDTO) throws URISyntaxException {
        log.debug("REST request to save TemplateProperty : {}", templatePropertyDTO);
        if (templatePropertyDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new templateProperty cannot already have an ID")).body(null);
        }
        TemplatePropertyDTO result = templatePropertyService.save(templatePropertyDTO);
        return ResponseEntity.created(new URI("/api/template-properties/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /template-properties : Updates an existing templateProperty.
     *
     * @param templatePropertyDTO the templatePropertyDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated templatePropertyDTO,
     * or with status 400 (Bad Request) if the templatePropertyDTO is not valid,
     * or with status 500 (Internal Server Error) if the templatePropertyDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/template-properties")
    @Timed
    public ResponseEntity<TemplatePropertyDTO> updateTemplateProperty(@Valid @RequestBody TemplatePropertyDTO templatePropertyDTO) throws URISyntaxException {
        log.debug("REST request to update TemplateProperty : {}", templatePropertyDTO);
        if (templatePropertyDTO.getId() == null) {
            return createTemplateProperty(templatePropertyDTO);
        }
        TemplatePropertyDTO result = templatePropertyService.save(templatePropertyDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, templatePropertyDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /template-properties : get all the templateProperties.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of templateProperties in body
     */
    @GetMapping("/template-properties")
    @Timed
    public ResponseEntity<List<TemplatePropertyDTO>> getAllTemplateProperties(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of TemplateProperties");
        Page<TemplatePropertyDTO> page = templatePropertyService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/template-properties");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /template-properties/:id : get the "id" templateProperty.
     *
     * @param id the id of the templatePropertyDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the templatePropertyDTO, or with status 404 (Not Found)
     */
    @GetMapping("/template-properties/{id}")
    @Timed
    public ResponseEntity<TemplatePropertyDTO> getTemplateProperty(@PathVariable Long id) {
        log.debug("REST request to get TemplateProperty : {}", id);
        TemplatePropertyDTO templatePropertyDTO = templatePropertyService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(templatePropertyDTO));
    }

    /**
     * DELETE  /template-properties/:id : delete the "id" templateProperty.
     *
     * @param id the id of the templatePropertyDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/template-properties/{id}")
    @Timed
    public ResponseEntity<Void> deleteTemplateProperty(@PathVariable Long id) {
        log.debug("REST request to delete TemplateProperty : {}", id);
        templatePropertyService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
